package com.justnow.offer;

public class Solution4 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {

        int length01 = nums1.length;
        int length02 = nums2.length;
        int[] all = new int[length01 + length02];
        double result = 0.0;
        int allLength = length02 + length01;
        int middle = allLength / 2;
        boolean flag = true;
        if (allLength % 2 == 0) {
            flag = true;
        } else {
            flag = false;
        }
        int i1 = 0;
        int i2 = 0;
        int index = 0;
        while ((i1 < length01) && (i2 < length02)) {
            if (nums1[i1] > nums2[i2]) {
                all[index] = nums2[i2];
                i2++;
            } else {
                all[index] = nums1[i1];
                i1++;
            }
            index++;
        }
        if (i1 != length01) {
            for (int i = i1; i < length01; i++) {
                all[index] = nums1[i];
                index++;
            }
        }
        if (i2 != length02) {
            for (int i = i2; i < length02; i++) {
                all[index] = nums2[i];
                index++;
            }
        }
        if (flag == true) {
            result = (double) (all[middle] + all[middle - 1]) / 2;
        } else {
            result = all[middle];
        }
        return result;
    }

    public static void main(String[] args) {
        Solution4 solution4 = new Solution4();
        double medianSortedArrays = solution4.findMedianSortedArrays(new int[]{1, 2}, new int[]{3, 4});
        System.out.println(medianSortedArrays);
    }
}
